﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using AutoMapper;

namespace DAO
{
    public class LoaiDocGiaRepository
    {
        public static LOAI_DOCGIA CreateLoaiDocGiaEFFromLoaiDocGiaDTO(LoaiDocGiaDTO LoaiDGDTO)
        {
            LOAI_DOCGIA LoaiDGEF = new LOAI_DOCGIA();
            Mapper.CreateMap<LoaiDocGiaDTO, LOAI_DOCGIA>();
            LoaiDGEF = Mapper.Map<LoaiDocGiaDTO, LOAI_DOCGIA>(LoaiDGDTO);
            return LoaiDGEF;
        }

        public static LoaiDocGiaDTO CreateLoaiDocGiaDTOFromLoaiDocGiaEF(LOAI_DOCGIA LoaiDGEF)
        {
            LoaiDocGiaDTO LoaiDGDTO = new LoaiDocGiaDTO();
            Mapper.CreateMap<LOAI_DOCGIA, LoaiDocGiaDTO>();
            LoaiDGDTO = Mapper.Map<LOAI_DOCGIA, LoaiDocGiaDTO>(LoaiDGEF);
            return LoaiDGDTO;
        }

        public List<LoaiDocGiaDTO>  LayDanhSachLoaiDocGia()
        {
            List<LoaiDocGiaDTO> ListLDG = new List<LoaiDocGiaDTO>();
            try
            {
                QLTVEntities context = new QLTVEntities();
                var query = from LOAI_DOCGIA in context.LOAI_DOCGIAList
                            select LOAI_DOCGIA;
                
                foreach (var LoaiDG in query)
                {
                    LoaiDocGiaDTO LoaiDGDTO = new LoaiDocGiaDTO();
                    
                    LoaiDGDTO = CreateLoaiDocGiaDTOFromLoaiDocGiaEF(LoaiDG);
                    ListLDG.Add(LoaiDGDTO);
                }

            }
            catch (System.Exception)
            { 
            }           
            
            return ListLDG;               

        }        

        public LoaiDocGiaDTO LayThongTinLoaiDocGia(int maLoaiDG)
        {
            LoaiDocGiaDTO loaiDGDTO = new LoaiDocGiaDTO();
            try
            {
                QLTVEntities context = new QLTVEntities();
                var query = from loaiDocGia in context.LOAI_DOCGIAList
                            where loaiDocGia.MaLoaiDocGia == maLoaiDG
                            select loaiDocGia;
                LOAI_DOCGIA loaiDGTemp = query.First();
                if (loaiDGTemp != null)
                    loaiDGDTO = CreateLoaiDocGiaDTOFromLoaiDocGiaEF(loaiDGTemp);
            }
            catch (System.Exception)
            {
            }
            return loaiDGDTO;
        }

        public void Them(LoaiDocGiaDTO LoaiDGDTO)
        {
            QLTVEntities context = new QLTVEntities();
            LOAI_DOCGIA LoaiDGEF = CreateLoaiDocGiaEFFromLoaiDocGiaDTO(LoaiDGDTO);

            context.AddToLOAI_DOCGIAList(LoaiDGEF);
            context.SaveChanges();

        }
        
    }
}
